package com.teejdeej.simpleneuralgame;

import java.util.List;

import org.newdawn.slick.Input;

public class HumanPlayer implements Player {
	private Input m_input = new Input(MainGame.ScreenHeight * MainGame.GridDimension);

	@Override
	public void learn(List<int[][]> boards, List<int[]> positions, List<Move> moves) {
	}

	@Override
	public Move move(int[][] board, int[] position) {
		Move t_out = new Move();
		
		if(m_input.isKeyDown(Input.KEY_UP))
		{
			t_out.m_move = true;
			t_out.m_moveDirection = MainGame.DirectionUp;
		}
		else if(m_input.isKeyDown(Input.KEY_DOWN))
		{
			t_out.m_move = true;
			t_out.m_moveDirection = MainGame.DirectionDown;
		}
		else if(m_input.isKeyDown(Input.KEY_LEFT))
		{
			t_out.m_move = true;
			t_out.m_moveDirection = MainGame.DirectionLeft;
		}
		else if(m_input.isKeyDown(Input.KEY_RIGHT))
		{
			t_out.m_move = true;
			t_out.m_moveDirection = MainGame.DirectionRight;
		}
		
		if(m_input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
		{
			t_out.m_fire = true;
			
			int[] t_screenPosition = new int[2];
			
			t_screenPosition[0] = position[0] * MainGame.GridDimension + MainGame.GridDimension / 2;
			t_screenPosition[1] = position[1] * MainGame.GridDimension + MainGame.GridDimension / 2;
			
			int[] t_relativeMousePosition = new int[2];
			
			t_relativeMousePosition[0] = m_input.getAbsoluteMouseX() - t_screenPosition[0];
			t_relativeMousePosition[1] = m_input.getAbsoluteMouseY() - t_screenPosition[1];
			
			double t_angleToMouse;
			if(t_relativeMousePosition[1] == 0)
			{
				t_angleToMouse = 0.0;
			}
			else
			{
				t_angleToMouse = Math.atan(t_relativeMousePosition[0] / t_relativeMousePosition[1]);
			}
			
			if(t_angleToMouse >= Math.PI / 8 && t_angleToMouse <= Math.PI / 8 * 3)
			{
				t_out.m_fireDirection = MainGame.DirectionUpRight;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 3 && t_angleToMouse <= Math.PI / 8 * 5)
			{
				t_out.m_fireDirection = MainGame.DirectionUp;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 5 && t_angleToMouse <= Math.PI / 8 * 7)
			{
				t_out.m_fireDirection = MainGame.DirectionUpLeft;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 7 && t_angleToMouse <= Math.PI / 8 * 9)
			{
				t_out.m_fireDirection = MainGame.DirectionLeft;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 9 && t_angleToMouse <= Math.PI / 8 * 11)
			{
				t_out.m_fireDirection = MainGame.DirectionDownLeft;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 11 && t_angleToMouse <= Math.PI / 8 * 13)
			{
				t_out.m_fireDirection = MainGame.DirectionDown;
			}
			else if(t_angleToMouse >= Math.PI / 8 * 13 && t_angleToMouse <= Math.PI / 8 * 15)
			{
				t_out.m_fireDirection = MainGame.DirectionDownRight;
			}
			else
			{
				t_out.m_fireDirection = MainGame.DirectionRight;
			}
		}
		
		return t_out;
		
	}

}
